<template>
  <div class="app-container">
    <el-select v-model="selectValue" clearable placeholder="请先选择数据连接池" @change="selectChange">
      <el-option
        v-for="item in datasource"
        :key="item.id"
        :label="item.datasourceName"
        :value="item.id">
        <span style="float: left;color: #121315;font-size: 15px">{{ item.datasourceName }}</span>
      </el-option>
    </el-select>
    <el-form :model="queryParams" ref="queryForm" :inline="true">
      <el-form-item label="" prop="keyword">
        <el-input v-model="queryParams.keyword" placeholder="请输入关键词搜索库表" clearable/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="getTableAliasList">搜索库表</el-button>
      </el-form-item>
    </el-form>

    <el-table
      ref="singleTable"
      highlight-current-row
      @current-change="handleCurrentChange"
      :data="tableData"
      height="550"
      border
      style="width: 100%">

      <el-table-column
        prop="tableName"
        label="数据表名称"
        width="135">
      </el-table-column>
      <el-table-column
        prop="tableNameAlias"
        label="数据表别名"
        width="140">
      </el-table-column>

    </el-table>

  </div>
</template>

<script>
import { getDatasource, getTableAliasEnable } from '@/api/quota/define/define.js'
import Sortable from 'sortablejs'

export default {
  name: 'tableAlias',
  components: {},
  props: {},
  data() {
    return {
      confirmSlot: undefined,
      selectValue: '',
      datasource: [],
      tableData: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderField: 'UPDATE_DATE',
        order:"desc",
        datasourceId: undefined,
        keyword: undefined
      }
    }
  },
  created() {
    this.getDatasourceList()
  },
  mounted() {
    // 阻止默认行为
    document.body.ondrop = function(event) {
      event.preventDefault()
      event.stopPropagation()
    }
    this.rowDrop()
    //this.columnDrop()
  },
  methods: {
    //行拖拽
    rowDrop() {
      const tbody = document.querySelector('.el-table__body-wrapper tbody')
      const _this = this
      Sortable.create(tbody, {
        onEnd({ newIndex, oldIndex }) {
          const currRow = _this.tableData.splice(oldIndex, 1)[0]
          _this.handleCurrentChange(currRow)
          _this.tableData.splice(newIndex, 0, currRow)
        }
      })
    },
    //重查询表单
    resetQuery() {
      this.resetForm('queryForm')
      this.getTableAliasList()
    },
    //选中的值
    selectChange(val) {
      this.queryParams.datasourceId = val
      this.getTableAliasList()
    },
    //加载表别名数据
    getTableAliasList() {
      if (this.queryParams.datasourceId == undefined) {
        this.msgError('请先选择数据源')
      } else {
        getTableAliasEnable(this.queryParams).then((res) => {
          this.tableData = res.data
        })
      }
    },
    //加载数据源数据
    getDatasourceList() {
      getDatasource().then((res) => {
        this.datasource = res.data
      })
    },
    handleCurrentChange(val) {
      let data = {
        tablenameAliasId: val.id
      }
      this.$emit('childData', data)
    }
  }
}
</script>

<style scoped>

</style>
